package com.scalar.chiptrack.workflow;

import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.JApplet;

public class RunAdaptorApplet extends JApplet implements Runnable{

	private URLConnection con = null;
	private String contextURL = null;
	
	//Response type
	private String response = "";
	private final String RES_COMPLETE = "complete";
	private final String REQ_END = "end";
	private final boolean DEBUG = true;
	
	private ObjectOutputStream inputToCommunicator = null;
	//private ObjectInputStream outputFromCommunicator = null;
	
	private BufferedReader outputFromCommunicator = null;
	
	public void init(){contextURL = getParameter("contextURL");}
	public void paint(Graphics g){}
	public void start(){}
	public void stop(){}
	
	public String startCommunicator(){
		
		con = getURLConnection();
		if(con != null){
			return "success";
		}
		
		return "failure";
	}
	
	public void startRequest(String vendorName){
		
		if(DEBUG){
			System.out.println("startRequest()");
		}
		
		try{
			
			inputToCommunicator = new ObjectOutputStream(con.getOutputStream());
			inputToCommunicator.writeObject(vendorName);
			inputToCommunicator.flush();
		
			//outputFromCommunicator = new ObjectInputStream(con.getInputStream());
			outputFromCommunicator = new BufferedReader(new InputStreamReader(con.getInputStream()));
			
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	/*public String receiveResponse(){
		
		if(DEBUG){
			System.out.println("receiveResponse()");
		}
		
		//for(;;){	
			try{
				
				response = (String)outputFromCommunicator.readObject();
				if(DEBUG){
					System.out.println("receiveResponse() + response = "+response);
				}
				
		//		if( RES_COMPLETE.equalsIgnoreCase(response) ){
		//			break;
		//		}
				
				//Thread.sleep(100);
				
			}catch(Exception e){
				e.printStackTrace();
				//break;
			}
		//}
		
		return response;
	}*/
	
	public String receiveResponse(){
		
		if(DEBUG){
			//System.out.println("receiveResponse()");
			System.out.println(response);
		}
		
		return response;
	}
	
	public boolean waitForResponse(){
		try{
			if((response = outputFromCommunicator.readLine()) != null){
				return true;
			}
		}catch(IOException e){
			e.printStackTrace();
		}
		
		return false;
	}
	
	public String endRequest(){
		
		try{
			
			if(null != inputToCommunicator){
				inputToCommunicator.close();
			}
			if(null != outputFromCommunicator ){
				outputFromCommunicator.close();
			}
			if(null != con){
				con = null;
			}
			
			response = "";
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return REQ_END;
	}
	
	public URLConnection getURLConnection(){
		
		URL url;
		URLConnection conn = null;
		
		try
		{
			url = new URL( this.getContextURL() + "/RunAdaptorCommunicator");
			conn = url.openConnection();
			
			if ( !conn.getDoOutput() )
			{
				conn.setDoOutput( true );
			}
			
			conn.setDoInput( true );
			conn.setUseCaches ( false );
			conn.setDefaultUseCaches ( false );
			conn.setAllowUserInteraction( true );
			conn.connect();
			
		}catch (IOException e){
			e.printStackTrace();
		}
		
		return conn;
	}
	
	public String getContextURL(){
		return contextURL;
	}
	
	public void run(){}
}